<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class ZlcrContentCheck extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr_content_check';

    protected $cacheModel = 'custom_report.content_check';


    public function saveData($data)
    {
        try
        {
            $this->save([
                'type' => $data['type'],
                'status' => $data['status'],
                'qid' => $data['qid'],
                'create_time' => $data['create_time']
            ]);

            // Log::write('占星师内容审核写入sql:' . $this->getLastSql(), 'log', true);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $data['qid'] . ':' . $data['type'])->rmCache();
        }
        catch (\Exception $e)
        {
            Log::write('占星师内容审核写入失败；写入数据' . json_encode($data, JSON_UNESCAPED_UNICODE) . ";错误信息：「{$e->getMessage()}」;错误文件「{$e->getFile()}」;行数「{$e->getLine()}」", true);
            return false;
        }
        return true;
    }


    /**
     * 获取内容审核信息
     * @param $qid
     * @param int $type
     * @param array $field
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getContentCheckInfo($qid, int $type, array $field = [])
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $qid . ':' . $type)
            ->getDataAndSet(function () use ($qid, $type)
            {
                try
                {
                    return $this->where(['qid' => $qid, 'type' => $type])->order('id desc')->findOrFail()->toArray();
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, $field);
    }
}
